import sys

user_passwd = {}
#字典结构为{用户名:[密码,电话号码,存款]}
#user_passwd = {'mxc': ['1', '123', 5000], 'mxc1': ['1', '123', 5000]}


# 登录
def log():
    wrong_user_count = 0
    wrong_passwd_count = 0
    while (wrong_user_count != 3) & (wrong_passwd_count != 3):

        pre_user_passwd = input('输入user和passwd，用空格隔开。如需注册，请输入“@r”:')

        if pre_user_passwd == "@r":

            pre = input('请注册新的user、passwd、电话号码，用空格隔开:')
            if len(list(pre.split())) != 3:
                print("Need user、passwd、tel!")
                continue
            user, passwd, tel = pre.split()
            user_passwd.update({user: [passwd, tel, 5000]})

            continue
        else:
            if len(list(pre_user_passwd.split())) != 2:
                print("Only need user and passwd!")
                continue
            user, passwd = (pre_user_passwd).split()

        if user not in user_passwd.keys():
            print("wrong user")
            wrong_user_count += 1
            continue
        elif user_passwd[user][0] != passwd:
            print("wrong passwd")
            wrong_passwd_count += 1
            continue
        elif user_passwd[user][0] == passwd:
            print("Logging in successful!")
            print(user_passwd)
            # 登录成功时返回用户名
            return user

    return False


# 查询余额
def check(user):
    return user_passwd[user][2]


# 取款
def draw_money(user, money):
    if int(money) > int(user_passwd[user][2]):
        print("没这么多钱！！")
        return False
    else:
        user_passwd[user][2] = int(user_passwd[user][2]) - int(money)
        print('余额为{}元'.format(check(user)))
        return True

# 存入
def save_money(user, money):
    user_passwd[user][2] = int(user_passwd[user][2]) + int(money)
    print('成功向{}存入{}元'.format(user, money))

def main():
    print("helloworld")

    user = log()
    while user:
        print()
        print("请输入对应功能代码：")
        print("1：查询余额")
        print("2：取款")
        print("3：转账")
        print("4：退出")
        code = input('>>>')
        if code == "1":
            print('余额为{}元'.format(check(user)))

        elif code == "2":
            print("请输入取款金额：")
            money = input('>>>')
            draw_money(user, money)

        elif code == "3":
            while True:
                pre = input('请输入对方user和转账金额，用空格隔开:')
                if len(list(pre.split())) != 2:
                    continue
                other_user, money = pre.split()
                if other_user == user:
                    print("不能给自己转账！")
                    continue
                if other_user not in user_passwd.keys():
                    print("对方用户不存在！")
                    continue
                if draw_money(user, money):
                    save_money(other_user, money)
                else:
                    continue
                print(user_passwd)
                break

        elif code == "4":
            break
        else:
            print("Wrong code!")


if __name__ == '__main__':
    main()
#  逻辑上没有啥问题，不过有个小疑问：sys 这个模块没有用到吧？